package com.teangtang.fly.service;


import com.teangtang.fly.dto.TravellerDTO;
import com.teangtang.fly.entity.Traveller;
import com.teangtang.fly.repository.TravellerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TravellerService {

    @Autowired
    private TravellerRepository travellerRepository;
    public Traveller findByPhone(String phone){
        return travellerRepository.findByPhone(phone);
    }
    public Traveller save(Traveller traveller){
        return travellerRepository.save(traveller);
    }
    public List<Traveller> getAllTravellers() {
        return travellerRepository.findAll();
    }
    public Traveller getTravellerById(String travellerId) {
        return travellerRepository.findById(travellerId).orElse(null);
    }

    public TravellerDTO createTraveller(TravellerDTO travellerDTO) {
        Traveller traveller = new Traveller();
        if(travellerRepository.findById(travellerDTO.getIdCard())!=null){
            traveller = travellerRepository.findById(travellerDTO.getIdCard()).get();
        }
        traveller.setPhone(travellerDTO.getPhone());
        traveller.setPassword(travellerDTO.getPassword());
        traveller.setName(travellerDTO.getName());
        traveller.setUsername(travellerDTO.getUsername());
        traveller.setGender(travellerDTO.getGender());
        traveller.setIdCard(travellerDTO.getIdCard());
        traveller.setIdCardType(travellerDTO.getIdCardType());
        traveller.setStatus(travellerDTO.getStatus());
        this.save(traveller);
        return travellerDTO;
    }

    public Traveller findByIdCardAndIdCardType(String idCard, String idCardType) {
        return travellerRepository.findByIdCardAndIdCardType(idCard, idCardType);
    }

    public void deleteTraveller(String travellerId) {
        travellerRepository.deleteById(travellerId);
    }
}
